package com.github.kezhenxu94.crawler.source.rss;

import com.alibaba.fastjson.JSONObject;
import com.github.kezhenxu94.crawler.source.rss.Rss2Channel;
import com.github.kezhenxu94.crawler.source.rss.Rss2Handler;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;

/**
 * Created by kezhenxu on 12/4/16.
 */
public class RssFetcher {

    public static void main(String[] args) throws Exception {
        pdress();
    }

    private static void xinhuanet() throws IOException, ParserConfigurationException, SAXException {
        String rssUrl = "http://www.xinhuanet.com/politics/news_politics.xml";
        String pattern = "EEE, dd-MMM-yyyy HH:mm:ss Z";

        Rss2Channel channel = getChannel(rssUrl, pattern);
        System.out.println(JSONObject.toJSONString(channel));
    }

    private static Rss2Channel getChannel(String rssUrl, String pattern) throws IOException, ParserConfigurationException, SAXException {
        URL url = new URL(rssUrl);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36");

        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        Rss2Handler handler = new Rss2Handler();
        handler.setDateFormat(new SimpleDateFormat(pattern));
        parser.parse(connection.getInputStream(), handler);
        return handler.getChannel();
    }

    private static void pdress() throws IOException, ParserConfigurationException, SAXException {
        String rssUrl = "https://p-dress.jp/articles/newest.rss";
        Rss2Channel channel = getChannel(rssUrl, "yyyy-MM-dd HH:mm:ss Z");
        System.out.println(JSONObject.toJSONString(channel));
    }

    private static void beijingReview() throws IOException, ParserConfigurationException, SAXException {
        String rssUrl = "http://japanese.beijingreview.com.cn/RSS1/index.html";
        Rss2Channel channel = getChannel(rssUrl, "yyyy-MM-dd HH:mm:ss");
        System.out.println(JSONObject.toJSONString(channel));
    }

    private static void logmi() throws ParserConfigurationException, SAXException, IOException {
        String rssUrl = "logmi.jp/feed/public.xml";
        Rss2Channel channel = getChannel(rssUrl, "yyyy-MM-dd'T'HH:mm:ssXXX");
        System.out.println(JSONObject.toJSONString(channel));
    }
}
